import measuringPhaseEnum from "../enum/measuringPhase.enum"
import { DragEndEvent } from "../event/DragEvent"
import { LongPressEndEvent, LongPressStartEvent } from "../event/GestureEvent"
import MeasurementCreator from "./MeasurementCreator"
import Ne from "../packages/76870"
import { InputSubscription } from "../module/InputIniModule"
import ConstrainedHit from "../utils/ConstrainedHit"
export default class MeasurementCreatorMobile extends MeasurementCreator {
  setCreatePointProgress: Function
  updateCreatePointHit: Function
  toggleCameraMovement: any
  continuous: () => string
  onLongPressSuccess: (e: any) => void
  onLongPressStart: (e: any) => void
  creatingPointTimeout: number
  onLongPressEnd: () => void
  onDrag: () => void
  onDragEnd: () => void
  dragSub: InputSubscription
  dragEndSub: InputSubscription
  constructor(e, t, i, s, n, r, o, a, h, l, d, c, u, p) {
    super(e, t, s, n, h, d, c, u, p)
    this.setCreatePointProgress = r
    this.updateCreatePointHit = o
    this.toggleCameraMovement = a
    this.continuous = l
    this.onLongPressSuccess = e => {
      this.log.debug("onLongPressSuccess while in phase:", measuringPhaseEnum[this.getPhase()])
      this.getPhase() === measuringPhaseEnum.CREATING
        ? (this.createNewLine(e), this.addPointToLine(e))
        : this.getPhase() === measuringPhaseEnum.CREATING_NEXT_POINT &&
          (this.continuous() ? (this.updateLastPoint(e), this.addPointToLine(e)) : this.updateLastPoint(e))
      this.setPhase(measuringPhaseEnum.POINT_PLACED)
    }
    this.onLongPressStart = e => {
      if (e.buttons === Ne.r3.PRIMARY && (this.getPhase() === measuringPhaseEnum.CREATING || this.getPhase() === measuringPhaseEnum.CREATING_NEXT_POINT)) {
        const e = this.getIntersection()
        if (e) {
          if (!this.previousPoint.equals(e.point)) {
            this.setPhase(measuringPhaseEnum.CONFIRMING_POINT)
            this.toggleCameraMovement(!1)
            this.setCreatePointProgress(Date.now(), MeasurementCreatorMobile.longPressCreateThreshold)
            this.updateCreatePointHit(e)
            this.previousPhase === measuringPhaseEnum.CREATING_NEXT_POINT && (this.updateLastPoint(e), this.toggleLastPointDraggable(!1))
            this.creatingPointTimeout = window.setTimeout(() => {
              this.restorePreviousPhase()
              this.onLongPressSuccess(e)
              this.toggleLastPointDraggable(!0)
            }, MeasurementCreatorMobile.longPressCreateThreshold)
          }
        }
      }
    }
    this.onLongPressEnd = () => {
      this.getPhase() === measuringPhaseEnum.CONFIRMING_POINT &&
        (this.log.debug("onLongPressEnd, cancelling confirmation"),
        this.setCreatePointProgress(0, MeasurementCreatorMobile.longPressCreateThreshold),
        window.clearTimeout(this.creatingPointTimeout),
        this.toggleCameraMovement(!0),
        this.restorePreviousPhase()),
        this.getPhase() === measuringPhaseEnum.CREATING_NEXT_POINT && this.points.update(this.points.length - 1, this.points.get(this.points.length - 2)),
        this.setPlacedtoCreatePhase()
    }
    this.onDrag = () => {
      const e = this.getIntersection()
      e &&
        (this.setPlacedtoCreatePhase(),
        this.setPhase(measuringPhaseEnum.EDITING),
        this.previousPhase === measuringPhaseEnum.CREATING_NEXT_POINT
          ? this.updateLastTwoPoints(e)
          : this.previousPhase === measuringPhaseEnum.CREATING && this.updateLastPoint(e))
    }
    this.onDragEnd = () => {
      this.getPhase() === measuringPhaseEnum.EDITING && this.restorePreviousPhase(),
        (this.getPhase() !== measuringPhaseEnum.CREATING && this.getPhase() !== measuringPhaseEnum.CREATING_NEXT_POINT) ||
          (this.toggleLastPointDraggable(!1), this.toggleCameraMovement(!0))
    }
    this.toggleLastPointDraggable = e => {
      e ? (this.dragSub.renew(), this.dragEndSub.renew()) : (this.dragSub.cancel(), this.dragEndSub.cancel())
    }
    this.updateLastTwoPoints = e => {
      this.points.update(this.points.length - 1, e.point), this.points.update(this.points.length - 2, e.point)
    }
    this.dragSub = i(DragEvent, this.onDrag)
    this.dragEndSub = i(DragEndEvent, this.onDragEnd)
    this.inputSubscriptions.push(i(LongPressStartEvent, this.onLongPressStart), i(LongPressEndEvent, this.onLongPressEnd))
    this.dragSub.cancel()
    this.dragEndSub.cancel()
    this.cancelSubs()
  }

  start() {
    super.start()
  }
  stop() {
    if (this.getPhase() === measuringPhaseEnum.CREATING_NEXT_POINT)
      if (this.continuous()) this.points.pop()
      else {
        const e = this.points.getGroup(this.currentGroup)
        this.points.removeFromIdx(e.startIndex)
      }
    super.stop()
  }
  setPlacedtoCreatePhase() {
    this.getPhase() === measuringPhaseEnum.POINT_PLACED &&
      (this.continuous()
        ? (this.previousPhase === measuringPhaseEnum.CREATING || this.previousPhase === measuringPhaseEnum.CREATING_NEXT_POINT) &&
          this.setPhase(measuringPhaseEnum.CREATING_NEXT_POINT)
        : this.previousPhase === measuringPhaseEnum.CREATING
        ? this.setPhase(measuringPhaseEnum.CREATING_NEXT_POINT)
        : this.previousPhase === measuringPhaseEnum.CREATING_NEXT_POINT && this.setPhase(measuringPhaseEnum.CREATING))
  }
  updateLastTwoPoints: (e: ConstrainedHit) => void
  toggleLastPointDraggable: (e) => void
  static longPressCreateThreshold: number
}
MeasurementCreatorMobile.longPressCreateThreshold = 500
